﻿using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Xml.Serialization;

namespace Jacobi.Seesmic.FeedReader.Model.Extensions
{
    /// <summary>
    /// Reads the 'content' tag from the 'http://search.yahoo.com/mrss/' namespace.
    /// </summary>
    [XmlRoot(ElementName = "content", Namespace = "http://search.yahoo.com/mrss/")]
    public sealed class YahooMediaContent
    {
        private static string Namepsace = "http://search.yahoo.com/mrss/";
        private static string Name = "content";

        private static XmlSerializer _serializer = null;

        /// <summary>
        /// Type of medium of the content.
        /// </summary>
        [XmlAttribute(AttributeName = "medium")]
        public string Medium { get; set; }

        /// <summary>
        /// The url of the content.
        /// </summary>
        [XmlAttribute(AttributeName = "url")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings",
            Justification = "By design")]
        public string Url { get; set; }

        /// <summary>
        /// Factory method for creating a collection of media links.
        /// </summary>
        /// <param name="extensions">Must not be null.</param>
        /// <returns>Returns null if not found.</returns>
        internal static IEnumerable<YahooMediaContent> Create(SyndicationElementExtensionCollection extensions)
        {
            if (_serializer == null)
            {
                _serializer = new XmlSerializer(typeof(YahooMediaContent));
            }

            return extensions.ReadElementExtensions<YahooMediaContent>(Name, Namepsace, _serializer);
        }
    }
}